package com.blog.controller;

import com.blog.common.CommonRespDto;
import com.blog.common.Constant;
import com.blog.common.RespBean;
import com.blog.config.CommonConfigPath;
import com.blog.config.JwtProperties;
import com.blog.service.BaseRoleService;
import com.blog.service.FileSystemService;
import com.blog.service.HrService;
import com.blog.system.Hr;
import com.blog.system.Role;
import com.blog.utils.JwtVerifyUtil;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

//import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hr")
public class HrController {
    @Autowired
    HrService hrService;
    @Autowired
    BaseRoleService roleService;
    @Autowired
    JwtProperties jwtProperties;
    @Autowired
    CommonConfigPath commonConfigPath;

    @Autowired
    FileSystemService fileSystemService;
    @GetMapping("/selectAllHr")
    public List<Hr> selectAllHr(){
        return hrService.selectAllHr();
    }
    @GetMapping("/getCommonFilePath")
    public String getCommonFilePath(){
        return  commonConfigPath.getFileAddress();
    }
    @GetMapping("/")
    public List<Hr> getAllHrs(HttpServletRequest request, String keywords){
        Map<String, Object> jwtHr = new JwtVerifyUtil().parseJwt(request.getHeader(Constant.TOKEN),jwtProperties.getSecretKey(),jwtProperties.getValidateInMs());
        List<Hr> hrList = hrService.getAllHrs((Integer) jwtHr.get("id"),keywords);
        for(Hr hr :hrList){
            if (!hr.getUserface().startsWith("http")){
                String projectRoot = System.getProperty("user.dir");
                //System.out.println(projectRoot);
                hr.setUserface(hr.getUserface().substring(1));
            }
        }
        return hrList;
    }
    @PutMapping("/")
    public RespBean updateHr(@RequestBody Hr hr){
        if(hrService.updateHr(hr)==1){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return roleService.getAllRoles();
    }
    @PutMapping("/role")
    public RespBean updateHrRole(Integer hrid,Integer[] rids){
        if(hrService.updateHrRole(hrid,rids)){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteHrById(@PathVariable Integer id){
        if(hrService.deleteHrById(id)==1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @PostMapping("/data/upload")
    public CommonRespDto getImg(MultipartFile file){
        if (file.isEmpty()) {
            CommonRespDto.error("请选择文件");
        }
        String path = null;
        try {
            path = fileSystemService.uploadFile("/user_face/"+file.getOriginalFilename(),file);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(!StringUtils.isEmpty(path)){
            return CommonRespDto.success(path,"上传成功");
        }else{
            return CommonRespDto.error("上传失败");
        }
    }

}
